// 引入 ECharts 主模块
var echarts = require('echarts/lib/echarts')
// 引入柱状图
require('echarts/lib/chart/bar')
require('echarts/lib/chart/line')
// 引入提示框和标题组件
require('echarts/lib/component/tooltip')
require('echarts/lib/component/title')
require('echarts/lib/component/legend')
require('echarts/lib/component/grid')
import { parseTime } from '@/utils/index'
const getLastDays = (nowDate: any, dayStand: number) => {
  let tempDate = new Date(nowDate)
  let tempArrData = []
  for (let i = 0; i < dayStand; i++) {
    tempArrData.unshift(
      parseTime(tempDate.getTime() - i * 24 * 60 * 60 * 1000, '{m}/{d}')
    )
  }

  return tempArrData
}
const getRandomDataPercent = (dayStand: number) => {
  let tempDate = Array(dayStand).fill(100.0)
  for (let i = 0; i <= Math.floor(dayStand / 4); i++) {
    tempDate[Math.floor(Math.random() * dayStand)] = (
      99.8 +
      0.2 * Math.random()
    ).toFixed(2)
  }
  return tempDate
}
const getRandomDataNum = (dayStand: number) => {
  let tempDate = []
  for (let i = 0; i <= dayStand; i++) {
    tempDate.push(Math.ceil(100 * Math.random()))
  }
  return tempDate
}
export default class EchartsConfig {
  private myChart: any
  private option = {
    title: {
      text: '',
      subtext: '',
      x: 'left'
    },
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b} : {c} ({d}%)'
    },
    color: ['#F4D761', '#3973F6', '#7844D7', '#DF6C7D', '#34A6D2', '#eb3a53'],
    legend: {
      orient: 'horizontal',
      x: 'center',
      bottom: '15%',
      data: [],
      icon: 'circle',
      textStyle: {
        color: [
          '#F4D761',
          '#3973F6',
          '#7844D7',
          '#DF6C7D',
          '#34A6D2',
          '#eb3a53'
        ]
      }
    },
    series: [
      {
        name: '地域分布占比',
        type: 'pie',
        radius: '45%',
        center: ['50%', '35%'],
        data: [],
        itemStyle: {
          borderWidth: 1,
          borderColor: '#ffffff',
          legendHoverLink: false
        }
      }
    ]
  }

  constructor(domId: string) {
    this.myChart = echarts.init(document.getElementById(domId))
  }
  public setOption(data: any, legend: any) {
    this.option.legend.data = legend
    this.option.series[0].data = data
    this.myChart && this.myChart.setOption(this.option)
    var _this = this
    window.addEventListener('resize', function() {
      _this.myChart.resize()
    })
  }
}
